import json,requests
import os
import re
import time
from jsonpath import jsonpath
from bs4 import BeautifulSoup
from datetime import datetime
# dt = datetime.fromisoformat("2020-10-20T17:23:00Z".replace("Z", "+00:00"))
# https://spa4.scrape.center/api/news/?limit=10&offset=0
class Movies():
    def __init__(self,i):
        self.url = f'https://spa5.scrape.center/api/book/?limit=18&offset={i}'
        self.headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}

    def send_re(self,url):
        response = requests.get(url, headers=self.headers)
        return response

    def send_img(self,url):
        response = requests.get(url, headers=self.headers)
        return response


    def jiexi(self,result,page):
        result_json = result.json()
        # 定位“电影”区域（根据网页结构，可能需要更新）
        print(result_json)
        movies_json = jsonpath(result_json, '$.results[*]')

        movies = []
        # 遍历每部电影卡片
        for item in movies_json:
            author_s = []
            title_name = jsonpath(item, '$.name')[0]

            authors = jsonpath(item, '$.authors')[0]
            print(title_name,authors)
            title_url = jsonpath(item, '$.cover')[0]

            if title_url:
                img_result = self.send_img(title_url)

                with open(f'./img/{title_name[:4]}.png','wb') as f:

                    f.write(img_result.content)
                    print(f'已保存{title_name}')
            else:
                title_url = 'wulianjie'

            for author in authors:
                author_s.append(re.sub(r'\s+', ' ', author).strip())

            if title_name:
                movies.append({
                    'page_title': f'第{page}页',
                    'title_name':title_name,
                    'authors': author_s,
                    'title_url': title_url
                })

        return movies

if __name__ == '__main__':
    '''
            1、先清空文件
            2、在写入
            3、再读、
            4、再追加
            '''
    os.remove('./result.json')
    for i in range(0, 5):
        #     print(i)
        m = Movies(i * 18)
        result = m.send_re(m.url)
        aa = m.jiexi(result, i)
        print(f'第{i}页结束')
        # 1. 读旧数据（若文件不存在则初始化为 []）
        if os.path.exists('result.json'):
            with open('result.json', 'r', encoding='utf-8') as f:
                data = json.load(f)
        else:
            data = []
        if isinstance(data, list):
            data.extend(aa)
        else:
            data.append(aa)

        with open('result.json', 'w+', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
        time.sleep(1)



